<template>
<div style="height:100%;position:relative;overfollow:hidden">
    <!-- 文章列表面包屑 -->
    <topCatalog v-on:addNewFile="addNewFileFn" :fileNum="filenum" v-on:filelistData='filelistDataFn' v-on:searchResult='searchFn' v-on:getnewFilepath='getnewFilepath'></topCatalog>
    <div style="height:calc(100% - 49px)">
      <!-- 文件操作栏 操作区 -->
        <div class="top-title ">
            <p v-if="isShowOnefile">公司文件</p>
            <p v-if="isShowdelfile">
              <span v-for="e in delfileData" :class="e.status == 1?'spanHover':''" @click="delFile(e)">{{e.title}}</span>
            </p>
            <div class="flex">
            <span>已选中{{filenum}}个文件</span>
            <div class="Newbuild" v-if="isShowupBtn">
              上传文件
              <ul class="newbuildHover">
                <li @click="uploadFileFn" :style="filenum != 0?'cursor: not-allowed;':''">
                  上传文件
                  <input type="file" multiple class="input-file" ref="uploadinfile" @change="uploadinput">
                  <!-- <input type="file" ref="uploadinfile" @change="uploadinput" webkitdirectory> -->
                </li>
                <li @click="uploadFolderFn" :style="filenum != 0?'cursor: not-allowed;':''">上传文件夹</li>
              </ul>
            </div>
            <div class="Newbuild"  :style="filenum != 0?'cursor: not-allowed;':''" @click="setNewFileFn" v-if="isShowNewfile">
            新建文件夹
            </div>
            <div class="Newbuild otherNewbuild" :style="filenum == 0?'cursor: not-allowed;':''" @click="deleteFileFn">
            删除文件
            </div>
            <div class="Newbuild otherNewbuild" :style="filenum == 0?'cursor: not-allowed;':''" v-if="isShowPackfile" @click="downloadFilesFn">
            打包下载
            </div>
            <div class="Newbuild otherNewbuild" :style="filenum == 0?'cursor: not-allowed;':''" v-if="isShowrecycleBin" @click="reductionFileFn">
            还原文件
            </div>
            <!-- <div class="Newbuild otherNewbuild warnText">
            
            </div> -->
            </div>
        </div>
        <div class="tablebox" id='tableBox' @contextmenu="shownullMenu()">
          <!-- 文件列表 -->
          <table class="filelist">
              <tr class="tabletitle">
                  <th class="first" :style="{'width':isshowpoaition ? '38%':'50%'}">文件名</th>
                  <th class="second" v-if="isshowpoaition">原来位置</th>
                  <th class="third">
                      大小
                      <img src="../assets/img/filelist/tapbefore.png" alt="" @click="SizesortupFn" v-if="isshowSizeNext">
                      <img src="../assets/img/filelist/shangblue.png" alt="" @click="SizesortdownFn" v-if="!isshowSizeNext">
                  </th>
                  <th class="forth" v-if="isShowdelfile == false">更新者</th>
                  <th class="forth" v-if="isShowdelfile">删除者</th>
                  <th class="fifth" v-if='isShowdelfile == false'>
                      更新时间
                      <img src="../assets/img/filelist/tapbefore.png" alt="" @click="TimesortupFn" v-if="isshowTimeNext">
                      <img src="../assets/img/filelist/shangblue.png" alt="" @click="TimesortdownFn" v-if="!isshowTimeNext">
                  </th>
                  <th class="fifth" v-if="isShowdelfile">
                      删除时间
                      <img src="../assets/img/filelist/tapbefore.png" alt="" @click="TimesortupFn" v-if="isshowTimeNext">
                      <img src="../assets/img/filelist/shangblue.png" alt="" @click="TimesortdownFn" v-if="!isshowTimeNext">
                  </th>
              </tr>
              <tr class="tabletitle tabletitlecontent" v-for="(item,index) in filelistData" @dblclick="toNextPageFn(item.type,item.name,item.id,item)" @contextmenu.prevent.stop="showMenu(item,$event)" @click="chooseFileFn(index)">
                  <td class="upload flex flex-pack-justify"> 
                    <div class="flex">
                      <span class="taplogo"><i :class="item.status == 1?'tapicon':''"></i></span>     
                       <!-- 文件logo  -->
                      <img src="../assets/img/filelist/logo.png" alt="" v-if="item.type == 'dir'" class="fileImg">

                      <img src="../assets/img/filelist/jpg.png" alt="" v-if="item.ext_type == 'photo'" style="width:20px;height:23px">
                      <img src="../assets/img/filelist/ppt.png" alt="" v-if="item.ext_type == 'ppt'">
                      <img src="../assets/img/filelist/video.png" alt="" v-if="item.ext_type == 'video'">
                      <img src="../assets/img/filelist/word.png" alt="" v-if="item.ext_type == 'word'">
                      <img src="../assets/img/filelist/excel.png" alt="" v-if="item.ext_type == 'execl'" style="width:20px;height:23px">

                      <img src="../assets/img/filelist/PDF.png" alt="" v-if="item.ext_type == 'pdf'" style="width:20px;height:23px">
                      <img src="../assets/img/filelist/project.png" alt="" v-if="item.ext_type == 'project'" style="width:20px;height:23px">
                      <img src="../assets/img/filelist/web.png" alt="" v-if="item.ext_type == 'web'" style="width:20px;height:23px">
                      <img src="../assets/img/filelist/other.png" alt="" v-if="item.ext_type == 'other'" style="width:20px;height:23px">
                      <img src="../assets/img/filelist/zip.png" alt="" v-if="item.ext_type == 'zip'" style="width:20px;height:23px">
                      <img src="../assets/img/filelist/txt.png" alt="" v-if="item.ext_type == 'txt'" style="width:20px;height:23px">
                      <img src="../assets/img/filelist/audio.png" alt="" v-if="item.ext_type == 'audio'" style="width:20px;height:23px">
                      <img src="../assets/img/filelist/CAD.png" alt="" v-if="item.ext_type == 'cad'" style="width:20px;height:23px">



                      <span class="filename" v-if="inputID != item.id">
                        <p :title="item.name">{{item.name}}</p>
                      </span>
                      <div class="renameinput" v-if="inputID == item.id">
                          <input type="text" class="editor"  v-model='item.name' id="myInput" v-on:input ="getTextFn"/>
                          <!-- @change="getTextFn($event)" -->
                          <span @click="saveNewnameFn(item.name)" style="color:#1185FF">保存</span>
                          <span @click="closeNameFn()">关闭</span>
                      </div>
                    </div>
                    <div class="operationIcon" v-if="isShowdelfile == false">
                        <img src="../assets/img/filelist/download.png" alt="" class="downloadIcon" @click="downloadImgFn(index)" v-if="item.type !== 'dir'">
                        <img src="../assets/img/filelist/note.png" alt="" class="noteIcon" @click.stop="renameImgFn(index)">
                        <img src="../assets/img/filelist/person.png" alt="" class="noteIcon">
                    </div> 
                  </td>
                  <td v-if="isshowpoaition" class="fileposition">
                    <p :title="item.route">{{item.route}}</p>
                  </td>
                  <td>
                    <span class="filesize" v-if="item.type != 'dir'">{{item.size}}</span>
                  </td>
                  <td class="fileposition-name">
                      <p :title="item.memberName" v-if="isShowdelfile == false">{{item.memberName}}</p>
                    <span v-if="isShowdelfile">{{item.delete_person}}</span>
                  </td>
                  <td>
                    <span v-if="isShowdelfile == false">{{item.date}}</span>
                    <span v-if="isShowdelfile">{{item.delete_time}}</span>
                  </td>
              </tr>
          </table>
          <div class = "menu">
            <!-- 鼠标右击菜单 -->
              <vue-context-menu :contextMenuData="contextMenuData" @open="openFn" @download="downloadFn" @del="delFn" @copy='copyFn' @shear='shearFn' @rename='renameFn' @setpermissions='setpermissionsFn' @viewproperties='viewpropertiesFn' @paste='pasteFn'></vue-context-menu>
          </div>
          
        </div>
        <!-- 底部bottom共计加载项 -->
        <div class="bottombox">
          已全部加载，共{{dataLength}}项
        </div>
    </div>
    
    <transition name="custom-classes-transition" enter-active-class="animated bounceInRight" leave-active-class="animated bounceOutRight">
        <authSlider v-if='is_auth' v-on:close="onCloseAuth"/>
    </transition>
    <div class="inter" :class="is_upload?'inter-index':''">
      <div class="bounceInter" :class="is_upload?'enter':''" >
        <uploadBox v-on:close="onCloseUpload" />
      </div>
    </div>
</div>
</template>

<script>
import topCatalog from "../components/common/topCatalog";
import authSlider from "../components/filepanel/authSlider";
import uploadBox from "../components/upload/uploadBox";
import PostFile from "../assets/js/postFile.js"
export default {
  components: {
    topCatalog,
    authSlider,
    uploadBox
  },
  data() {
    return {
      stup:true,
      dataLength: null, //共多少项文件
      isShowdelfile: false, //判断是否显示垃圾站中全部and我删除的
      delfileData: [
        { title: "全部", status: "1" },
        { title: "我删除的", status: "0" }
      ],
      isShowPackfile: false, //判断是否显示打包下载按钮
      isShowNewfile: false, // 判断是否显示新建文件按钮
      isShowrecycleBin: false, //判断路由是否是垃圾站，是否显示还原文件按钮
      isShowOnefile: false, //是否在按钮栏显示'公司文件'名称
      isShowupBtn: false, //控制是否显示上传按钮
      filelistData: [], //文件列表数据
      inputID: null, //控制重命名input显示变量
      isshowSizeNext: true, //点击切换大小排序logo
      isshowTimeNext: true, //点击切换大小排序logo
      filenum: 0, //选中的文件数量
      selectArr: [], //被选中的文件数据
      // 右击
      contextMenuData: {
        menuName: "demo",
        axis: {
          x: null,
          y: null
        },
        menulists: []
      }, //鼠标右击菜单数据
      rightSelect: {}, //鼠标右击选中的list
      oldName: "", //未修改的文件名
      copyAndshear: false, //点击复制剪切右击菜单显示
      isshowpoaition: false, //当路由跳转回收站后表格显示文件原来位置
      is_auth: false, //权限设置右方滑块
      is_upload:false,    //上传时下方弹框
      rootpath: "", //根文件路径
      filepath: [], //文件夹名称路径
      currentfile: "/" ,//当前文件夹名称拼接之后地址
      isRenameOrNewfile:false,
      isAutoFocus:false,
      permission:'',       //用户操作权限
      type:null,     
      copyfilepath:'',      //粘贴文件的地址
      page:'1',     //回收站页数
      retype:'1',      //回收站获取全部以及我删除的文件
      id:'',        //回收站文件ID
      searchValue:{},     //搜索type以及内容
      order:'',      //文件排序
    };
  },
  watch: {
    type() {
      if(this.type == 1){
        this.isshowFn();
        this.isShowNewfile = true;
        this.isShowupBtn = true;
        this.isShowPackfile = true;
      }else if(this.type == 2){
        this.isshowFn();
        this.isShowNewfile = true;
        this.isShowPackfile = true;
      }else if(this.type == 3){
        this.isshowFn();
        this.isShowdelfile = true;
        this.isShowrecycleBin = true;
        this.isshowpoaition = true;
      }
    },
    sureMethod() {
      // 遮罩点击确定文件操作
      switch (this.sureMethod) {
        case "suredel":
          this.delCurFilefn();
          // 调用确定删除文件接口
          if(sessionStorage.getItem('type') == 3){
              this.recycledeleteFn();
          }else{
            if(this.permission.indexOf('permanent_delete') != -1){
              this.filedelete();
            }else{
              this.warnToastFn()
              return;
            }
          }
          
          break;
        case "surehuan":
          this.delCurFilefn();
          // 调用确定还原文件接口
          this.backfileFn()
          break;
      }
      this.$store.commit("getSure", "");
    },
  },
  computed: {
    sureMethod() {
      return this.$store.state.sureMethod;
    },
  },
  updated(){
    if(this.isAutoFocus){
      document.getElementById('myInput').focus();
      document.getElementById('myInput').select();
    }
  },
  mounted() {  
    if(sessionStorage.getItem('path')){
      this.filepath = sessionStorage.getItem('path').split('/')
    }
    // else{
    //   this.filepath = '/'
    // }
    this.isShowNewfile = true;
    this.isShowPackfile = true;
    //  判断当前页面层级 调用接口获取数据
    this.filedata();
    this.Operationfn();
    this.scroll()
  },
  methods: {
    scroll() {
      let _self = this;
      let scrollDiv = document.getElementById("tableBox");
      scrollDiv.addEventListener("scroll", function() {
        if (
          scrollDiv.scrollTop + scrollDiv.clientHeight ==
            scrollDiv.scrollHeight &&
          _self.stup
        ) {
          _self.page++;
          // console.log(sessionStorage.getItem('type'))
          if(sessionStorage.getItem('type') == 3){
            _self.recycleList()
          }else{
            var data = "";
            if (sessionStorage.getItem("path") == "" || sessionStorage.getItem("path") == null) {
              data = "/";
            } else {
              data = "/" + sessionStorage.getItem("path");
            }
            _self.filefloderdata(data)
          }
          // _self.filefloderdata();
        }
      });
    },
    // 操作日志
    async Operationfn(){
      var myDate = new Date();   
      var time = myDate.getFullYear() + '-' + myDate.getMonth() + '-' + myDate.getDate() + ' ' + myDate.getHours() + ":" + myDate.getMinutes() + ":" + myDate.getSeconds()
      // var params = {
      //   time:time,
      //   operating_type:"",
      //   user_nick:''
      // }
    },
    // 点击左侧文件 初始化
    async filedata() {
      this.type = sessionStorage.getItem('type')
      var params = {
        type: sessionStorage.getItem('type')?sessionStorage.getItem('type'):'2'
      };
      if(sessionStorage.getItem('type') == 3){
        this.recycleList()
      }else{
        var res = await this.$https.post("explorer", params);
        this.rootpath = res.data.rootDir;
        var data = "";
        if (sessionStorage.getItem("path") == "" || sessionStorage.getItem("path") == null) {
          data = "/";
        } else {
          data = "/" + sessionStorage.getItem("path");
        }
        this.filefloderdata(data);
      }
      
    },
    // 获取文件列表
    async filefloderdata(path) {
      var params = { 
        path: path,
        page:this.page,
        _order:this.order,
        ext_type:this.searchValue.type,
        search:this.searchValue.content
      };
      var res = await this.$https.post("File/ListFile", params);
      if (res.success) {
        // this.searchValue = {}
        if(res.data.result.length != 0){
          for (let i in res.data.result) {
            this.filelistData.push(res.data.result[i]);
            this.dataLength = this.filelistData.length;
          }
          this.stup = true
        }else{
          this.stup = false
          // if(this.id != ''){
          //   // this.filePathFn(fileName);
          //   this.filelistData = res.data.list
          //   console.log(this.filelistData)
          // }
        }

        // this.filelistData = res.data.result;
        // console.log(this.filelistData)
        // this.dataLength = this.filelistData.length;
        this.permission = res.data.dep_permissions;
      }
    },
    // 回收站列表
    async recycleList(){
      // console.log(this.searchValue.content)
      var params = {
        id:this.id,
        type:this.retype,
        limit:'20',
        page:this.page,
        search:this.searchValue.content,
        order:this.order
      }
      var res = await this.$https.post("Recycle/getRecycleList",params);
      if(res.success){
        // this.filelistData = res.data.list;
        // console.log(res.data.list)
        if(res.data.list.length != 0){
          for (let i in res.data.list) {
            this.filelistData.push(res.data.list[i]);
            this.dataLength = this.filelistData.length;
          }
          this.stup = true
        }else{
          this.stup = false
          if(this.id != ''){
            // this.filePathFn(fileName);
            this.filelistData = res.data.list
            // console.log(this.filelistData)
          }
        }
      }
    },
    // 回收站删除文件操作
    async recycledeleteFn(){
      var ids = [];
      this.selectArr.forEach( e => {
        ids.push(e.id)
      })
      var params = {
        ids:ids
      };
      var res = await this.$https.post('Recycle/deleteRecycle',params)
      // console.log(res)
      if(res.success){
        this.filelistData = []
        this.page = '1'
        this.recycleList()
      }
    },
    // 回收站还原文件操作
    async backfileFn(){
      var ids = [];
      this.selectArr.forEach( e => {
        ids.push(e.id)
      })
      var params = {
        ids:ids
      };
      var res = await this.$https.post('Recycle/reductionRecycle',params)
      if(res.success){
        this.filelistData = []
        this.page = '1'
        this.recycleList()
      }
    },
    // 单文件下载
    async downloadfile(path, type, id) {
      var params = {
        path: path
      };
      var res = await this.$https.post("File/Download?action=download", params);
      if (res.success) {
        location.href = 'http://39.108.180.1:10094/index.php' + res.data.url
      }
    },
    // 操作区文件打包下载
    downloadFilesFn(){
      if (this.filenum == 0) {
        return;
      } else {
        var str = '';
        this.selectArr.forEach( e => {
          if (sessionStorage.getItem("path") == "") {
            var data = "/";
          } else {
            var data = "/" + sessionStorage.getItem("path") + '/';
          }
          str += data + e.name + ","
        })
        str=str.substring(0,str.length-1)
        // console.log(str)
        this.downloadfiles(str)
      }
    },
    // 多文件下载
    async downloadfiles(path) {
      var params = {
        paths: path
      };
      var res = await this.$https.post("/File/zipDownloadFiles", params);
      // console.log(res);
      if(res.success){
      window.location.href = 'http://39.108.180.1:10094/' + res.data.url
      }
    },
    // 删除文件接口
    async filedelete() {
      var arr = [];
      var filearr = [];
      this.selectArr.forEach(item => {
        if (sessionStorage.getItem("path") == "") {
          var r = "/" + item.name;
          var e = "/" + item.name;
        } else {
          var r = "/" + sessionStorage.getItem("path") + "/" + item.name;
          var e = "/" + sessionStorage.getItem("path") + "/" + item.name;
        }
        if (item.type == "dir") {
          arr.push(r);
          r = "";
        } else {
          filearr.push(e);
          e = "";
        }
      });
      var params = {
        items: filearr,
        dirs: arr
      };
      var res = await this.$https.post("File/Delete", params);
      this.selectArr = [];
      if (res.success == true) {
        var str = "/" + sessionStorage.getItem("path");
        this.filefloderdata(str);
      }
    },
    // 点击面包屑文件路径
    filelistDataFn(data) {
      this.filelistData = data;
    },
    // 控制显示变量初始化
    isshowFn() {
      this.isShowNewfile = false;
      this.isShowPackfile = false;
      this.isShowrecycleBin = false;
      this.isShowdelfile = false;
      this.isShowupBtn = false;
      this.isshowpoaition = false;
    },
    // 停止上传文件
    stopUploadFn() {},
    // 点击上传文件
    uploadFileFn() {
      if (this.filenum != 0) {
        return;
      }
      this.is_upload = true;
      // 上传文件
      // PostFile()
    },
    uploadinput(){
      console.log(this.$refs.uploadinfile.files)
      var file = this.$refs.uploadinfile.files[0]
      var i = 1
      PostFile(file,i)
    },
    // 点击上传文件夹
    uploadFolderFn() {
      if (this.filenum != 0) {
        return;
      }
      this.is_upload = true;
    },
    // 上传进度条进程
    // onspeed() {
    //   let self = this;
    //   var timer = window.setInterval(() => {
    //     self.speedSize++;
    //     if (self.speedSize == 100) {
    //       clearInterval(timer);
    //       // 调用上传文件的接口，根据上传结果赋给this.uploadText相应的值
    //       this.uploadText = "上传失败";
    //       this.wordColor = "#E45C5B";
    //     }
    //   }, 600);
    // },
    // 回收站btn栏tabs点击事件
    delFile(e) {
      this.filelistData = [];
      this.searchValue = {};
      this.delfileData.forEach(item => {
        if (item.title == e.title) {
          item.status = 1;
          if(e.title == '我删除的'){
            this.retype = '2'
            this.recycleList()
          }else if(e.title == '全部'){
            this.retype = '1'
            this.recycleList()
          }
        } else {
          item.status = 0;
        }
      });
    },
    // 点击搜索实现模糊搜索功能
    searchFn(data) {
      this.searchValue = data;
      this.filelistData = [];
      this.page = '1'
      // 调用接口
      if(sessionStorage.getItem('type') == '3'){
        // console.log(this.searchValue)
        this.recycleList()
      }else{
        if (sessionStorage.getItem("path") == "" || sessionStorage.getItem("path") == null) {
          var path = "/";
        } else {
          var path = "/" + sessionStorage.getItem("path");
        }
        this.filefloderdata(path)
      }
      // if (sessionStorage.getItem("path") == "" || sessionStorage.getItem("path") == null) {
      //   var path = "/";
      // } else {
      //   var path = "/" + sessionStorage.getItem("path");
      // }
      // var type = data.type
      // this.filefloderdata(path,type);
      // var arr = [];
      // // 执行获取文件列表函数
      // // this.filelistData = [];
      // this.filelistData.forEach(item => {
      //   if (item.name.indexOf(data.content) != -1) {
      //     arr.push(item);
      //   }
      // });
      // this.filelistData = arr
    },
    // 鼠标右击文件list
    showMenu(item,e) {
      if(sessionStorage.getItem('type') == 3){
        return;
      }else{
        this.isShowSelectFn();
        this.filenum += 1;
        item.status = 1;
        this.rightSelect = item;
        this.$store.commit("getlistData", this.rightSelect);
        this.selectArr.push(item);
        this.currentfile += item.name;
        e.preventDefault();
        var x = e.clientX;
        var y = e.clientY;
        // Get the current location
        this.contextMenuData.axis = {
          x,
          y
        };
        if(this.rightSelect.type == 'dir'){
          if(this.type == '1'){
            this.contextMenuData.menulists = [
              { fnHandler: "open", btnName: "打开" },
              { fnHandler: "rename", btnName: "重命名" },
              { fnHandler: "copy", btnName: "复制" },
              { fnHandler: "shear", btnName: "剪切" },
              { fnHandler: "del", btnName: "删除" },
              { fnHandler: "viewproperties", btnName: "查看属性" }
            ];
          }else if(this.type == '2'){
            this.contextMenuData.menulists = [
              { fnHandler: "open", btnName: "打开" },
              { fnHandler: "rename", btnName: "重命名" },
              { fnHandler: "copy", btnName: "复制" },
              { fnHandler: "shear", btnName: "剪切" },
              { fnHandler: "del", btnName: "删除" },
              { fnHandler: "setpermissions", btnName: "设置权限" },
              { fnHandler: "viewproperties", btnName: "查看属性" }
            ];
          }
        }else{
          this.contextMenuData.menulists = [
          { fnHandler: "open", btnName: "打开" },
          { fnHandler: "download", btnName: "下载" },
          { fnHandler: "rename", btnName: "重命名" },
          { fnHandler: "copy", btnName: "复制" },
          { fnHandler: "shear", btnName: "剪切" },
          { fnHandler: "del", btnName: "删除" },
          { fnHandler: "setpermissions", btnName: "设置权限" },
          { fnHandler: "viewproperties", btnName: "查看属性" }
        ];
        }
        
      }
      
    },
    // 鼠标右击空白处
    shownullMenu() {
      this.isShowSelectFn();
      event.preventDefault();
      var x = event.clientX;
      var y = event.clientY;
      // Get the current location
      this.contextMenuData.axis = {
        x,
        y
      };
      if (this.copyAndshear) {
        this.contextMenuData.menulists = [];
        this.contextMenuData.menulists.push({
          fnHandler: "paste",
          btnName: "粘贴"
        });
        return;
      } else {
        this.contextMenuData.menulists = [];
      }
    },
    // 点击还原文件
    reductionFileFn() {
      this.$store.commit('getSelectFn',this.selectArr)
      if (this.filenum == 0) {
        return;
      } else {
        this.gettoStoredata("reduction", true);
      }
    },
    // 文件选中状态清空为未选中状态
    isShowSelectFn() {
      this.filelistData.forEach((e, i) => {
        e.status = 0;
      });
      this.filenum = 0;
    },
    // 右击菜单中的打开
    openFn() {
      if(this.permission.indexOf('item_view') != -1){
        if (this.rightSelect.type == "dir") {
          var fileName = this.rightSelect.name;
          this.filenum = 0;
          this.filePathFn(fileName);
          this.openfloder();
        } else {
          return;
        } 
      }else{
        this.warnToastFn()
        return;
      }
    },
    // 右击菜单中的下载
    downloadFn() {
      var downloadpath = "";
      if (sessionStorage.getItem("path") == "") {
        downloadpath = "/" + this.rightSelect.name;
      } else {
        downloadpath =
          "/" + sessionStorage.getItem("path") + "/" + this.rightSelect.name;
      }
      this.downloadfile(downloadpath);
      // window.open(url); //文件下载
    },
    // 点击下载图标
    downloadImgFn(i){
      this.chooseFileFn(i);
      // 判断当前文件的文件类型
      if(this.rightSelect.type == 'dir'){
        // 下载文件夹
        // this.downloadFilesFn()
        return;
      }else{
        // 下载单个文件
        this.downloadFn()
      }
    },
    // 右击菜单中的删除
    delFn() {
      this.gettoStoredata("del", true);
    },
    // 右击菜单中的复制
    copyFn() {
      this.copyfile()
    },
    // 复制接口
    async copyfile(){
      if(this.permission.indexOf('item_copy') != -1){
        var copypath = "";
        if (sessionStorage.getItem("path") == "") {
          copypath = "/" + this.rightSelect.name;
        } else {
          copypath =
            "/" + sessionStorage.getItem("path") + "/" + this.rightSelect.name;
        }
        if(this.rightSelect.type == 'dir'){
          var type = 'dir'
        }else{
          var type = 'file'
        }
        this.copyfilepath = copypath
        var params = {
          action:'copy',
          id:this.rightSelect.id,
          item:copypath,
          type:type
        }
        var res = await this.$https.post('/File/Copy',params)
        if(res.success){
          this.copyAndshear = true;
        }
      }else{
        this.warnToastFn();
        return;
      }
      
    },
    // 右击菜单中的剪切
    shearFn() {
      this.shearfileFn()
    },
    // 剪切接口
    async shearfileFn(){
      var copypath = "";
      if (sessionStorage.getItem("path") == "") {
        copypath = "/" + this.rightSelect.name;
      } else {
        copypath =
          "/" + sessionStorage.getItem("path") + "/" + this.rightSelect.name;
      }
      this.copyfilepath = copypath;
      var params = {
        action:'cut',
        id:this.rightSelect.id,
        item:this.copyfilepath,
        type:this.rightSelect.type
      };
      var res = await this.$https.post('/File/Cut',params)
      if(res.success){
        this.copyAndshear = true;
      }
    },
    // 右击菜单中的粘贴
    pasteFn() {
      this.copyAndshearFn() 
    },
    // 粘贴接口
    async copyAndshearFn(){
      // var copypath = ''
      // if (sessionStorage.getItem("path") == "") {
      //   copypath = "/" + this.rightSelect.name;
      // } else {
      //   copypath =
      //     "/" + sessionStorage.getItem("path") + "/" + this.rightSelect.name;
      // }
      var params = {
        action:'Paste',
        path:"/" + sessionStorage.getItem("path")
      };
      var res = await this.$https.post('/File/Paste',params)
      if(res.success){
        this.copyAndshear = false;
        this.contextMenuData.menulists = [
          { fnHandler: "open", btnName: "打开" },
          { fnHandler: "download", btnName: "下载" },
          { fnHandler: "rename", btnName: "重命名" },
          { fnHandler: "copy", btnName: "复制" },
          { fnHandler: "shear", btnName: "剪切" },
          { fnHandler: "del", btnName: "删除" },
          { fnHandler: "setpermissions", btnName: "设置权限" },
          { fnHandler: "viewproperties", btnName: "查看属性" }
        ];
        var str = "/" + sessionStorage.getItem("path");
        this.filefloderdata(str);
      }
    },
    // 右击菜单中的重命名
    renameFn() {
      var itemID = this.rightSelect.id;
      this.oldName = this.rightSelect.name;
      this.isAutoFocus = true;
      if (this.inputID != itemID) {
        this.inputID = itemID;
      } else {
        this.inputID = null;
        // this.oldName = this.rightSelect.name;
      }
    },
    // 点击重命名图标
    renameImgFn(i){
      this.chooseFileFn(i)
      this.renameFn()
    },
    // 保存重命名
    saveNewnameFn(e) {
      // console.log(e)  this.oldName
      // 重命名接口 e为现用名
      if(this.permission.indexOf('item_mkrename') != -1){
        if(this.isRenameOrNewfile){
          this.addfile(e);
          this.isAutoFocus = false;
        }else{
          this.isAutoFocus = false;
          this.rename(e);
        }
      }else{
        this.warnToastFn();
        return;
      }      
    },
    async rename(e) {
      var oldname = "";
      var newname = "";
      if (sessionStorage.getItem("path") == "") {
        oldname = "/" + this.oldName;
        newname = "/" + e;
      } else {
        oldname = "/" + sessionStorage.getItem("path") + "/" + this.oldName;
        newname = "/" + sessionStorage.getItem("path") + "/" + e;
      }
      var params = {
        item: oldname,
        newItemPath: newname
      };
      var res = await this.$https.post("File/Rename", params);
      if (res.success) {
        if (sessionStorage.getItem("path") == "") {
          var path = "/";
        } else {
          var path = "/" + sessionStorage.getItem("path");
        }
        this.filefloderdata(path);
        this.filenum = 0;
        this.inputID = null;
        this.rightSelect.status = 0;
      }
    },
    // 关闭重命名
    closeNameFn() {
      this.filelistData.forEach(e => {
        if (e.id == this.rightSelect.id) {
          e.name = this.oldName;
        }
      });
      this.inputID = null;
      this.isAutoFocus = false;
    },
    // 右击菜单中的设置权限
    setpermissionsFn() {
      this.is_auth = true;
    },
    // 关闭设置权限滑块
    onCloseAuth() {
      this.is_auth = false;
    },
    // 关闭上传弹框
    onCloseUpload(){
      this.is_upload = false;
    },
    // 右击菜单中的查看属性
    viewpropertiesFn() {
      // this.$store.commit("isshowMaskType", "attribute");
      this.gettoStoredata("attribute", true);
    },
    // input 内容改变
    getTextFn() {
      this.isAutoFocus = false;
    },
    // 点击选择文件前icon
    chooseFileFn(index) {
      this.filelistData.forEach((item, key) => {
        if (key === index) {
          if (item.status == 1) {
            item.status = 0;
            this.filenum -= 1;
            this.selectArr.forEach((ele, i) => {
              if (ele.id == item.id) {
                this.selectArr.splice(i);
              }
            });
          } else {
            item.status = 1;
            this.filenum += 1;
            this.selectArr.push(this.filelistData[index]);
          }
          if(this.selectArr.length == 1){
            this.rightSelect = this.selectArr[0]
          }
        }
      });
    },
    // 操作区 点击新建文件
    setNewFileFn() {
      // 判断其是否有新建文件的权限
      // console.log(this.permission.indexOf('item_mk') != -1)
      if(this.permission.indexOf('item_mk') != -1){
        this.isRenameOrNewfile = true
        this.oldName = "";
        if (this.filenum != 0) {
          return;
        } else {
          // var data = "true";
          if (this.inputID === null) {
            this.addNewFileFn();
          }
        }
      }else{
        this.warnToastFn()
        return;
      }



      // if (true) {
      //   this.warnToastFn();
      // } else {
        // console.log(this.$refs.input);
        // this.$refs.input.setAttribute('autofocus',true);
        // this.isAutoFocus = true;
      
      // }
    },
    // 点击删除文件
    deleteFileFn() {
      if (this.filenum == 0) {
        return;
      } else {
        this.gettoStoredata("del", true);
      }
    },
    // input失去焦点
    lostBlurFn(e) {
      this.inputID = null;
    },
    // 面包屑一栏点击新建文件夹
    addNewFileFn() {
      let num = 0;
      let name = "";
      let filterArr = [];
      var arr = [];
      if (this.filelistData) {
        let filterArr = this.filelistData.filter(e =>
          e.name.includes("新建文件夹")
        );
        
        filterArr.forEach(item => {
          if (item.name.length == 5) {
            arr.push("0");
          } else {
            arr.push(item.name.substring(6, 7));
          }
        });
        for (var i = 0; i < arr.length; i++) {
          arr.find(item => {
            if (i != item) {
              num = i
            }else if(i == item){
              num = i + 1
            }
          });
        }
      }
      name = num ? "新建文件夹" + "(" + num + ")" : "新建文件夹";
      this.oldName = name;
      var obj = {
        id: true,
        name: name,
        type: "dir"
      };
      this.filelistData.splice(0, 0, obj);
      this.isAutoFocus = true;
      if (this.inputID != obj.id) {
        this.inputID = obj.id;
      } else {
        this.inputID = null;
      }
    },
    // 新建文件夹
    async addfile(name) {
      var path = "";
      if (sessionStorage.getItem("path") == "") {
        path = "/" + name;
      } else {
        path = "/" + sessionStorage.getItem("path") + "/" + name;
      }
      var params = { newPath: path };
      var res = await this.$https.post("File/createFolder", params);
      if(res.success){
        this.inputID = null;
        var data = "/" + sessionStorage.getItem("path")
        this.filefloderdata(data)
      }
    },
    // 点击按大小排序
    SizesortupFn() {
      // this.warnToastFn();
      this.isshowSizeNext = false;
      this.filelistData = []
      this.page = '1'
      // 按大小从小到大排序
      if(sessionStorage.getItem('type') == 3){
        this.order = 'size asc'
        this.recycleList()
      }else{
        this.order = 'size asc'
        var data = "";
        if (sessionStorage.getItem("path") == "" || sessionStorage.getItem("path") == null) {
          data = "/";
        } else {
          data = "/" + sessionStorage.getItem("path");
        }
        this.filefloderdata(data);
      } 
    },
    SizesortdownFn() {
      this.isshowSizeNext = true;
      this.filelistData = []
      this.page = '1'
      // 按大小从大到小排序
      if(sessionStorage.getItem('type') == 3){
        this.order = 'size desc'
        this.recycleList()
      }else{
        this.order = 'size desc'
        var data = "";
        if (sessionStorage.getItem("path") == "" || sessionStorage.getItem("path") == null) {
          data = "/";
        } else {
          data = "/" + sessionStorage.getItem("path");
        }
        this.filefloderdata(data);
      } 
    },
    // 点击按时间先后排序
    TimesortupFn() {
      this.isshowTimeNext = false;
      this.filelistData = []
      this.page = '1'
      if(sessionStorage.getItem('type') == 3){
        this.order = 'delete_time asc'
        this.recycleList()
      }else{
        this.order = 'date asc'
        var data = "";
        if (sessionStorage.getItem("path") == "" || sessionStorage.getItem("path") == null) {
          data = "/";
        } else {
          data = "/" + sessionStorage.getItem("path");
        }
        this.filefloderdata(data);
      } 
      
    },
    TimesortdownFn() {
      this.isshowTimeNext = true;
      this.filelistData = []
      this.page = '1'
      if(sessionStorage.getItem('type') == 3){
        this.order = 'delete_time desc'
        this.recycleList()
      }else{
        this.order = 'date desc'
        var data = "";
        if (sessionStorage.getItem("path") == "" || sessionStorage.getItem("path") == null) {
          data = "/";
        } else {
          data = "/" + sessionStorage.getItem("path");
        }
        this.filefloderdata(data);
      }
    },
    // 双击点击跳转下一级页面
    toNextPageFn(fileType, fileName,fileID,item) {
      // console.log(fileID)
      
      if(this.permission.indexOf('item_view') != -1){
        this.page = '1'
        if(sessionStorage.getItem('type') == 3){
          // 文件类型判断是否能双击打开
          if(item.type == 'dir'){
            this.filePathFn(fileName);
            this.id = item.old_id;  
            this.filelistData = []
            this.recycleList()
          }else{
            return false;
          }
          
        }else{
          if (fileType == "dir") {
            this.filelistData = []
            this.filePathFn(fileName);
            this.openfloder();
          } else {
            return;
          }
        }
      }else{
        this.warnToastFn()
        return;
      }      
    },
    // 打开文件夹
    openfloder() {
      var arr = this.$store.state.filePath;
      var str = "/";
      arr.forEach((item, index) => {
        str += item + "/";
      });
      if (str.length > 0) {
        str = str.substr(0, str.length - 1);
      }
      this.currentfile = str;
      this.filefloderdata(str);
    },
    // 点击按钮及右击跳出遮罩弹框 传往store的值
    gettoStoredata(e, v) {
      this.$store.commit("isshowMaskType", e);
      this.$store.commit("isshowMask", v);
    },
    // 所有文件设置为未选中状态，并删除当前选中的文件
    delCurFilefn() {
      this.filelistData.forEach((e, i) => {
        if (e.status == 1) {
          // this.filelistData.splice(i, this.filenum);
          this.filenum = 0;
        }
      });
    },
    // 黑框提示
    warnToastFn() {
      this.$toast({
        message: "无相关权限！",
        time: 3000
      });
    },
    getnewFilepath(data) {
      this.filepath = data;
    },
    // 文件地址拼接
    filePathFn(data) {
      // console.log(data)
      // console.log(sessionStorage.getItem('path') == '')
      if (sessionStorage.getItem('path') == '') {
        this.filepath = [];
      }
      this.filepath.push(data);
      this.$store.commit("getFilepath", this.filepath);
    }
  }
};
</script>

<style lang="less" scoped>
@import url(../assets/css/filepanel.less);
</style>

